﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
//using System.Threading.Tasks;

namespace UploadForm.Common
{
    public class Web
    {
        string baseUrl = "";

        public void GetFunction(string url)
        {

            string serviceAddress = "http://222.111.999.444:8687/tttr/usercrd/12/b7e50cb45a?userid=9999";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            //Response.Write(retString);
        }

        public void PostFunction(string url,string content)
        {



            //string serviceAddress = "http://222.111.999.444:8687/tttr/usercrd/uuu/12/dfd7e4";
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);

            //request.Method = "POST";
            //request.ContentType = "application/json";
            //string strContent = @"{ ""mmmm"": ""89e"",""nnnnnn"": ""0101943"",""kkkkkkk"": ""e8sodijf9""}";
            //using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
            //{
            //    dataStream.Write(strContent);
            //    dataStream.Close();
            //}
            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //string encoding = response.ContentEncoding;
            //if (encoding == null || encoding.Length < 1)
            //{
            //    encoding = "UTF-8"; //默认编码  
            //}
            //StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            //string retString = reader.ReadToEnd();
            ////解析josn
            //JObject jo = JObject.Parse(retString);
            //Response.Write(jo["message"]["mmmm"].ToString());

        }

        /// <summary>
        /// 向服务器端提交数据
        /// </summary>
        /// <param name="serverURL">服务器端地址</param>
        /// <param name="data">要发送的数据</param>
        /// <param name="parms">附加参数，用URL的方式将此参数附件到地址中</param>
        /// <returns>服务器端返回的 byte[]</returns>
        public static byte[] PostData(string serverURL, byte[] data, Hashtable parms)
        {
            System.Net.WebClient webClientObj = new System.Net.WebClient();

            if (parms != null)
            {
                serverURL = serverURL + "?";
                foreach (string key in parms.Keys)
                {
                    serverURL = serverURL + key + "=" + parms[key].ToString() + "&";

                }
                serverURL.TrimEnd('&');
            }
            byte[] byRemoteInfo = webClientObj.UploadData(serverURL, "POST", data);

            return byRemoteInfo;

        }

    }
}
